
package com.daily.pump.dao;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.daily.pump.app.App;
import com.daily.pump.model.Device;
import com.google.gson.reflect.TypeToken;
import com.xunxi.mimi.utils.GsonUtil;
import com.xunxi.mimi.utils.ListUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * SharedPreferences储存
 *
 * @author liyu
 */
public class DaoSharedPreferences {

    private static DaoSharedPreferences sInstance;

    private SharedPreferences mPreferences;

    private Editor mEditor;

    private static final String NAME = "PumpDb";

    private static final String DEVICE_CONFIG = "wifi_config";//WiFi信息

    private DaoSharedPreferences() {
        mPreferences = App.getInstance().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        mEditor = mPreferences.edit();
    }

    public static DaoSharedPreferences getInstance() {
        if (sInstance == null) {
            sInstance = new DaoSharedPreferences();
        }

        return sInstance;
    }

    /**
     * 清空数据
     */
    public void clear() {
        mEditor.clear();
        mEditor.apply();
    }

    public Device getLoginDevice() {
        Device userInfo = null;
        List<Device> wifiConfigList = getDeviceList();
        if (!ListUtil.isEmpty(wifiConfigList)){
            return wifiConfigList.get(0);
        }
        return userInfo;
    }

    public List<Device> getDeviceList(){
        String userInfoStr = mPreferences.getString(DEVICE_CONFIG, "");
        if (!TextUtils.isEmpty(userInfoStr)) {
            List<Device> userList = GsonUtil.parseList(userInfoStr, new TypeToken<List<Device>>() {
            }.getType());
            return userList;
        }
        return new ArrayList<>();
    }

    /**
     * 设置用户信息
     *
     * @param device
     */
    public void setDevice(Device device) {
        if (device == null) {
            return;
        }
        List<Device> wifiConfigList = getDeviceList(device);
        wifiConfigList.add(0, device);
        mEditor.putString(DEVICE_CONFIG, GsonUtil.toJson(wifiConfigList));
        mEditor.apply();
    }

    /**
     * Desc:获取WiFi连接列表
     * <p>
     * Author: [李豫]
     * Date: 2020-10-24
     *
     * @param device
     * @return list
     */
    private List<Device> getDeviceList(Device device) {
        List<Device> wifiConfigList = getDeviceList();
        for (int i = 0; i < wifiConfigList.size(); i++) {
            Device devicItem = wifiConfigList.get(i);
            if (TextUtils.equals(devicItem.deviceName, device.deviceName)){
                wifiConfigList.remove(i);
                return wifiConfigList;
            }
        }
        return wifiConfigList;
    }

    public void deleteDevice(Device scanResult) {
        List<Device> wifiConfigList = getDeviceList();
        for (int i = 0; i < wifiConfigList.size(); i++) {
            Device simpleWifiBean = wifiConfigList.get(i);
            if (TextUtils.equals(scanResult.deviceName, simpleWifiBean.deviceName)){
                wifiConfigList.remove(i);
                mEditor.putString(DEVICE_CONFIG, GsonUtil.toJson(wifiConfigList));
                mEditor.apply();
                return;
            }
        }
    }
}
